<!DOCTYPE html>

<!-- rAF throttling also is tested in fast/dom, but this test accounts for
  out-of-process iframes which are only created in http tests. -->
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<style>
#frame {
  position: absolute;
  top: -1000px;
}
</style>

<iframe id="frame" sandbox="allow-scripts" src="http://localhost:8000/dom/resources/raf-throttling-frame.html"></iframe>

<script>
if (window.testRunner)
  testRunner.dumpAsText();
let throttlingTest = async_test("Test requestAnimationFrame() throttling in out-of-view cross origin frames");

var frame = document.querySelector('#frame');
var state = 0;
var firstRafCount = 0;

frame.onload = () => {
  // Check how many animation frames have been served to the frame after it has
  // been given time to load.
  requestAnimationFrame(() => {
    setTimeout(() => {
      // By the time this runs, the frame should be in a steady state.
      var frame = document.querySelector('#frame');
      frame.contentWindow.postMessage(null, '*');
    }, 0)
  });
};

window.addEventListener('message', (e) => {
  if (state == 0) {
    firstRafCount = e.data;
    state = 1;
    // We don't know how many frames will have been served before they became
    // throttled, but the number shouldn't subsequently change.
    requestAnimationFrame(() => {
      setTimeout(() => {
        var frame = document.querySelector('#frame');
        frame.contentWindow.postMessage(null, '*');
      }, 0)
    });
  } else {
    let rafCount = e.data;
    throttlingTest.step(() => {
      assert_equals(rafCount, firstRafCount, "requestAnimationFrame() callback count")
    });
    throttlingTest.done();
  }
});
</script>
